import {config, request} from 'utils'

const {api} = config
const {
  arcgisFeatureServerQuery,
  saveNewLayerUrl
} = api

//保存
export function saveFeature(params) {
  const {url, fs} = params;
  return request(`${url}/addFeatures`, {
    data: {f: 'pjson', features: fs},
    method: 'POST',
    requestType: 'form'
  });
}

//删除
export function deleteFeature(params) {
  const {url, oid} = params;
  return request(`${url}/deleteFeatures`, {
    data: {
      f: 'pjson',
      objectIds: oid,
      geometryType: 'esriGeometryEnvelope',
      spatialRel: 'esriSpatialRelIntersects'
    },
    method: 'POST',
    requestType: 'form'
  })
}

//更新
export function updateFeature(params) {
  const {url, fs} = params;
  return request(`${url}/updateFeatures`, {
    data: {f: 'pjson', features: fs},
    method: 'POST',
    requestType: 'form'
  });
}

// 按关键词检索图层
export async function queryLayerByKeys(params) {
  const {url, keys} = params;
  let epsgCode = 4326;
  let where = ` status_cd=1 and name like '%${keys}%'`;
  const queryStr = `f=json&returnGeometry=true&inSR=${epsgCode}&outSR=${epsgCode}&outFields=*&where=${encodeURIComponent(where)}`;
  return request(`${url}/query?` + queryStr, {
    method: 'GET'
  });
}

//新增图层弹出编辑属性框
export function saveNewLayer(data) {
  return request(saveNewLayerUrl, {
    data,
    method: 'post',
  })
}
